<?php
/**
 * Html code of offer editing page (the one with forms).
 * 
 * @package templates
 */

	$cm = CodeManager::getCodeManager();

	if (!Policy::isAdminView())  {// TODO check for accessibility
		Error::getError()->addErrorMessage($cm->get("10005"));
		return;
	}
?>

<div id="offer-edit">
<?php
	$formActionString = "/index.php?";
	$formActionString .= Constants::REQ_ACTION."=".Constants::REQ_ACTION_REDIRECT;
	$formActionString .= "&amp;";
	$formActionString .= Constants::REQ_TYPE."=".Constants::REQ_TYPE_OFFEREDIT;
	
	
	
	$dbConnector = SiteContext::getSiteContext()->getDBConnector();
	
	$isedit = false;
	
	if(isset($_GET[Constants::REQ_TYPE]) && $_GET[Constants::REQ_TYPE] != Constants::REQ_TYPE_OFFEREDIT) {
		$id = (int)mysql_real_escape_string($_GET[Constants::REQ_TYPE], DBConnectorMySQL::getInitialResource());
		$res = $dbConnector->query("select * from offers where id = $id limit 1"); // Make constant from this query
		
		$arr = mysql_fetch_assoc($res); // TODO what if this throw an exception?
		foreach (Offer::$CODES_COLUMNS as $columnWithCode) {
			$res = $dbConnector->query("select * from codeslong where id = ".$arr[$columnWithCode]." limit 1"); // TODO do it without redundancy
			$code = mysql_fetch_assoc($res);
			foreach (SiteLang::$LANG_AVAILABLE as $lang => $unused) {
				$arr[Constants::DB_COMMON_CODESPREFIXFROMAFORM.$lang."_".$columnWithCode] = $code[$lang];
			}
		}
		$isedit = true;
		HtmlHelper::setValuesToInit($arr);
	} else {
		HtmlHelper::setValuesToInit($_POST);
		$id = (int)@$_POST['id'];
	}

?>
	<form onsubmit="return Common.validateOffer(this);" action="<?php echo $formActionString; ?>" method="post" enctype="multipart/form-data">
		<table>
		<tbody>
			<tr>
				<td class="left"><?php echo $cm->get("30013"); ?>: </td>
				<td class="right">
					<select class="large" name="locality">
						<?php 
							foreach (Definitions::$SETUP_LOCALITIES as $localityId => $localityName) {
								echo "<option " . HtmlHelper::selectedSelectWithAttr("locality", $localityId) . "> $localityName</option>";
							}
						?>
					</select>
				</td>
			</tr>
			<tr>
				<td class="left"><?php echo $cm->get("30014"); ?>: </td>
				<td class="right">
					<input type="text" <?php echo HtmlHelper::inputValueWithAttr("town"); ?>/>
				</td>
			</tr>
			<tr>
				<td class="left"><?php echo $cm->get("30015"); ?>: </td>
				<td class="right">
					<input type="text" <?php echo HtmlHelper::inputValueWithAttr("street"); ?>/>
				</td>
			</tr>
			<tr>
				<td class="left"><?php echo $cm->get("30098"); ?>: </td>
				<td class="right">
					<input onblur="Common.makeInt(this);" class="small" type="text" <?php echo HtmlHelper::inputValueWithAttr("zip"); ?>/>
				</td>
			</tr>
			<tr>
				<td class="left"><?php echo $cm->get("30084"); ?> <?php echo $cm->get("30097"); ?> <?php echo $cm->get("30085"); ?>: </td>
				<td class="right">
					<select class="small" name="rentOrSale">
						<option <?php echo HtmlHelper::selectedSelectWithAttr("rentOrSale", "rent"); ?>> rent </option>
						<option <?php echo HtmlHelper::selectedSelectWithAttr("rentOrSale", "sale"); ?>> sale </option>
					</select>
				</td>
			</tr>
			<tr>
				<td class="left"><?php echo $cm->get("30010"); ?>: </td>
				<td class="right">
					<input onblur="Common.makeFloat(this);" class="small" type="text" <?php echo HtmlHelper::inputValueWithAttr("price"); ?>/>
					<select class="small" name="currency">
						<option <?php echo HtmlHelper::selectedSelectWithAttr("currency", "czk"); ?>> CZK </option>
						<option <?php echo HtmlHelper::selectedSelectWithAttr("currency", "eur"); ?>> EUR </option>
					</select>
				</td>
			</tr>
			<tr>
				<td class="left"><?php echo $cm->get("30016"); ?>: </td>
				<td class="right">
					<input onblur="Common.makeFloat(this);" class="small" type="text" <?php echo HtmlHelper::inputValueWithAttr("size"); ?>/> m&sup2;
				</td>
			</tr>
			<tr>
				<td class="left"><?php echo $cm->get("30020"); ?>: </td>
				<td class="right">
					<select name="agentId">
						<?php 
							$logger = Logger::getLogger();
							$info = SiteContext::getSiteContext();
				
							$res = $info->getDBConnector()->query(Constants::DB_QUERY_USERS_ALL);
							
							while($user = User::mysqlFetch($res)) {
								Logger::getLogger()->debug("processing user: " . $user->id);
								$str = "$user->id - $user->firstName $user->surName ($user->login)";
								echo '
						<option '.HtmlHelper::selectedSelectWithAttr("agentId", $user->id).'> '.$str.' </option>';
							}
						?>
					</select>
				</td>
			</tr>
		</tbody>
		<tbody>
			<tr><td colspan="2" class="caption"><?php echo $cm->get("30096"); ?>:</td></tr>
			<tr>
				<td class="left"><?php echo $cm->get("30091"); ?>: </td>
				<td class="right">
					<input class="extra-large" type="text" <?php echo HtmlHelper::inputValueWithAttr(Constants::DB_COMMON_CODESPREFIXFROMAFORM."en_title"); ?>/><br/>
				</td>
			</tr>
			<tr>
				<td class="left"><?php echo $cm->get("30092"); ?>: </td>
				<td class="right">
					<input class="extra-large" type="text" <?php echo HtmlHelper::inputValueWithAttr(Constants::DB_COMMON_CODESPREFIXFROMAFORM."cs_title"); ?>/><br/>
				</td>
			</tr>
			<tr>
				<td class="left"><?php echo $cm->get("30093"); ?>: </td>
				<td class="right">
					<input class="extra-large" type="text" <?php echo HtmlHelper::inputValueWithAttr(Constants::DB_COMMON_CODESPREFIXFROMAFORM."de_title"); ?>/>
				</td>
			</tr>
			<tr><td colspan="2" class="caption"><?php echo $cm->get("30099"); ?>:</td></tr>
			<tr>
				<td class="left"><?php echo $cm->get("30091"); ?>: </td>
				<td class="right">
					<textarea name="<?php echo Constants::DB_COMMON_CODESPREFIXFROMAFORM."en_text"; ?>"><?php echo HtmlHelper::attributeValue(Constants::DB_COMMON_CODESPREFIXFROMAFORM."en_text"); ?></textarea>
				</td>
			</tr>
			<tr>
				<td class="left"><?php echo $cm->get("30092"); ?>: </td>
				<td class="right">
					<textarea name="<?php echo Constants::DB_COMMON_CODESPREFIXFROMAFORM."cs_text"; ?>"><?php echo HtmlHelper::attributeValue(Constants::DB_COMMON_CODESPREFIXFROMAFORM."cs_text"); ?></textarea>
				</td>
			</tr>
			<tr>
				<td class="left"><?php echo $cm->get("30093"); ?>: </td>
				<td class="right">
					<textarea name="<?php echo Constants::DB_COMMON_CODESPREFIXFROMAFORM."de_text"; ?>"><?php echo HtmlHelper::attributeValue(Constants::DB_COMMON_CODESPREFIXFROMAFORM."de_text"); ?></textarea>
				</td>
			</tr>
		</tbody>
<?php 
try {
	$res = $dbConnector->query("select * from offers where id = $id limit 1"); // Make constant from this query 
	if ($htmlOffer = HtmlOffer::fetchInstance($res)) {
?>
				<tbody>
				<tr>
				<td class="left vertical-spacer">
					<?php echo $cm->get("30021"); ?>:
				</td>
				<td class="vertical-spacer">
					(<?php echo $cm->get("30029"); ?>.)
				</td>
			</tr>
			<tr>
					<td colspan="2">
						<div class="pictures">
<?php 

		function processImage($object, $fileName) {
			$attrValue = Common::escapeThumbImgToAttr($fileName);
			$object->replaceTemplate("/_TEMPLATE_ADDED_CHECKBOX_/", HtmlHelper::selectedInputCheckboxWithAttr($attrValue));
		}
		
		
		$htmlCode = '
						<div class="thumbwrapped framed">
							<div class="swc0"><div class="swc1">
								<div class="swc2"> 
										<img src="_TEMPLATE_THUMBSRC_" alt="" />
								</div>
								<span class="swc3"/>
							</div></div>
						</div>
						<div class="checkbox-delete framed">
							<input class="checkbox" type="checkbox" _TEMPLATE_ADDED_CHECKBOX_ />
						</div>';
		$htmlOffer->processImages($htmlCode, 'processImage');
		$htmlOffer->flushMe();
?>
						<div class="cleaner"></div>
						</div>
				</td>
			</tr>
		</tbody>
<?php 
	}
} catch (ExceptionQueryFailed $e) {
}
?>
		<tbody>
			<tr id="_TEMPLATE_ADDIMG_" class="invisible">
				<td class="left">
					<?php echo $cm->get("30021"); ?>:
				</td>
				<td class="right">
					<!-- onblur="Common.conditionallyDeleteImgInput(this); return false;"  -->
					<input type="file" name="img1" />
				</td>
			</tr>
			<tr id="offer-edit-empty-one-image-input">
				<td class="left">
					<?php echo $cm->get("30021"); ?>:
				</td>
				<td class="right">
					<!-- onblur="Common.conditionallyDeleteImgInput(this); return false;"  -->
					<input type="file" name="img1" />
				</td>
			</tr>
		</tbody>
		<tbody>
			<tr>
				<td class="left">
				</td>
				<td class="right">
					<a href="javascript:Common.copyImgInput(this);"><?php echo $cm->get("30022"); ?></a> 
				</td>
			</tr>
			<tr>
				<td class="left">
					<?php echo $cm->get("30070"); ?>:
				</td>
				<td class="right">
					<input class="checkbox" type="checkbox" <?php echo HtmlHelper::selectedInputCheckboxWithAttr("applyWatermark"); ?> />
				</td>
			</tr>
			<tr>
				<td class="left">
					<?php echo $cm->get("30080"); ?>:
				</td>
				<td class="right">
					<input class="checkbox" type="checkbox" <?php echo HtmlHelper::selectedInputCheckboxWithAttr("top", "0"); ?> />
				</td>
			</tr>
			<tr>
				<td class="left">
					<?php echo $cm->get("30083") ." '". $cm->get("30081") . "'"; ?>:
				</td>
				<td class="right">
					<input class="checkbox" type="checkbox" <?php echo HtmlHelper::selectedInputCheckboxWithAttr("status", "visible"); ?> />
				</td>
			</tr>
			<tr>
				<td class="left">
					SEO url:
				</td>
				<td class="right">
					<input class="extra-large" type="text" <?php echo HtmlHelper::inputValueWithAttr("seoUrl"); ?> />
				</td>
			</tr>
			<tr>
				<td class="left">
					SEO description:
				</td>
				<td class="right">
					<input class="extra-large" type="text" <?php echo HtmlHelper::inputValueWithAttr("seoDescription"); ?> />
				</td>
			</tr>
			<tr>
				<td class="left">
					SEO keywords:
				</td>
				<td class="right">
					<input class="extra-large" type="text" <?php echo HtmlHelper::inputValueWithAttr("seoKeywords"); ?> />
				</td>
			</tr>
			<tr>
				<td colspan="2" class="submit">
<?php 
	$info = SiteContext::getSiteContext();
	$random = Common::createFormHash();
	$info->setFormHash($random);
?>
					<input type="text" class="invisible" <?php echo HtmlHelper::inputValueWithAttr("title"); ?>/>
					<input type="text" class="invisible" <?php echo HtmlHelper::inputValueWithAttr("text"); ?>/>
					<input type="text" class="invisible" <?php echo HtmlHelper::inputValueWithAttr("id"); ?>/>
					<input type="text" value="<?php echo $random; ?>" class="invisible" name="formHash" />
					<input class="submit middle" type="submit" value="<?php echo $cm->get("20005"); ?>" />
					<input class="clear middle" type="button" onclick="Common.clearForm(this.form)" value="<?php echo $cm->get("20007"); ?>" />
				</td>
			</tr>
		</tbody>
		</table>
	</form>
</div>